package com.meida.module.bus.provider.api.schedule;

import cn.hutool.log.StaticLog;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meida.module.bus.client.entity.BusMaterialPlan100;
import com.meida.module.bus.client.entity.BusSupplier;
import com.meida.module.bus.client.entity.srmapi.basedata.SrmVendor;
import com.meida.module.bus.provider.service.BusMaterialPlan100Service;
import com.meida.module.bus.provider.service.BusSupplierService;
import com.meida.module.bus.provider.service.srmservice.SrmSqlserCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {
    @Autowired
    private BusSupplierService busSupplierService;
    @Autowired
    private SrmSqlserCommonService srmSqlserCommonService;

    @Scheduled(cron = "0 */30 * * * ?")
    private void syncVendorInfoFromSrm() {
        //3.添加定时任务
        //或直接指定时间间隔，例如：5秒
        //@Scheduled(fixedRate=5000)
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
        StaticLog.info("syncVendorInfoFromSrm----start");
        List<BusSupplier> busSupplierList = busSupplierService.list();
        List<SrmVendor> srmVendorList = srmSqlserCommonService.getSrmVendor(Maps.newHashMap());
        if (0 < srmVendorList.size() && 0 < busSupplierList.size()) {
            StaticLog.info("syncVendorInfoFromSrm----判断开始");

            List<BusSupplier> busSuppliersForIns = Lists.newArrayList();
            List<BusSupplier> busSuppliersForUpd = Lists.newArrayList();
            srmVendorList.stream().forEach(srmVendorObj -> {
                if (null != srmVendorObj.getErpSynState() && BigDecimal.ONE.compareTo(srmVendorObj.getErpSynState()) == 0) {
                    if (!busSupplierList.stream().anyMatch(busSupplier -> busSupplier.getSupplierCode().equals(srmVendorObj.getVendorCode()))) {
                        BusSupplier busSupplier = new BusSupplier();
                        busSupplier.setSupplierCode(srmVendorObj.getVendorCode());
                        busSupplier.setSupplierName(srmVendorObj.getVendorName());
                        StaticLog.info("存在插入");
                        StaticLog.info("syncVendorInfoFromSrm--ins--srmVendorObj.getVendorCode()：" + srmVendorObj.getVendorCode());
                        StaticLog.info("syncVendorInfoFromSrm--ins--srmVendorObj.getVendorName()：" + srmVendorObj.getVendorName());
                        busSuppliersForIns.add(busSupplier);
                    } else {
                        BusSupplier busSupplierExists = busSupplierList.stream().filter(busSupplier -> busSupplier.getSupplierCode().equals(srmVendorObj.getVendorCode())).findFirst().get();
                        if (!busSupplierExists.getSupplierName().equals(srmVendorObj.getVendorName())) {
                            busSupplierExists.setSupplierName(srmVendorObj.getVendorName());
                            StaticLog.info("存在更新");
                            StaticLog.info("syncVendorInfoFromSrm--upd--busSupplierExists.getSupplierName()：" + busSupplierExists.getSupplierName());
                            StaticLog.info("syncVendorInfoFromSrm--upd--busSupplierExists.getSupplierCode()：" + busSupplierExists.getSupplierCode());
                            StaticLog.info("syncVendorInfoFromSrm--upd--srmVendorObj.getVendorCode()：" + srmVendorObj.getVendorCode());
                            StaticLog.info("syncVendorInfoFromSrm--upd--srmVendorObj.getVendorName()：" + srmVendorObj.getVendorName());
                            busSuppliersForUpd.add(busSupplierExists);
                        }
                    }
                }
            });
            if (0 < busSuppliersForIns.size()) {
                busSupplierService.saveBatch(busSuppliersForIns);
            }
            if (0 < busSuppliersForUpd.size()) {
                busSupplierService.updateBatchById(busSuppliersForUpd);
            }
        } else if (0 == busSupplierList.size()) {
            List<BusSupplier> busSuppliersForIns = Lists.newArrayList();
            srmVendorList.stream().forEach(srmVendorObj -> {
                if (null != srmVendorObj.getErpSynState() && BigDecimal.ONE.compareTo(srmVendorObj.getErpSynState()) == 0) {
                    BusSupplier busSupplier = new BusSupplier();
                    busSupplier.setSupplierCode(srmVendorObj.getVendorCode());
                    busSupplier.setSupplierName(srmVendorObj.getVendorName());
                    StaticLog.info("存在插入");
                    StaticLog.info("syncVendorInfoFromSrm--ins--srmVendorObj.getVendorCode()：" + srmVendorObj.getVendorCode());
                    StaticLog.info("syncVendorInfoFromSrm--ins--srmVendorObj.getVendorName()：" + srmVendorObj.getVendorName());
                    busSuppliersForIns.add(busSupplier);
                }
            });
            if (0 < busSuppliersForIns.size()) {
                busSupplierService.saveBatch(busSuppliersForIns);
            }
        }
        StaticLog.info("syncVendorInfoFromSrm----end");
    }
}
